package cn.yh.register.hosp.controller;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.MD5;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.model.hosp.HospitalSet;
import cn.yh.register.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

//@CrossOrigin    //解决跨域问题
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    HospitalSetService hospitalSetService;

    //1 查询医院设置表所有信息
    @ApiOperation("查询医院设置表所有信息")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAll() {
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    //2 逻辑删除医院设置
    @ApiOperation("逻辑删除医院设置")
    @ApiImplicitParam(name = "id", value = "医院id", dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public Result<?> removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //3 条件查询带分页
    @ApiOperation("条件查询带分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "queryVo", value = "查询条件", dataType = "HospitalSetQueryVo", paramType = "body")
    })
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findPageHosp(@PathVariable Integer current,
                                                  @PathVariable("limit") Integer size,
                                                  @RequestBody(required = false) HospitalSetQueryVo queryVo) {
        //构造page对象
        Page<HospitalSet> Page = new Page<>(current, size);
        Page<HospitalSet> hospitalSetPage;
        if (queryVo != null) {
            //构造条件查询
            LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(!StringUtils.isEmpty(queryVo.getHosname()), HospitalSet::getHosname, queryVo.getHosname())
                    .eq(!StringUtils.isEmpty(queryVo.getHoscode()), HospitalSet::getHoscode, queryVo.getHoscode());
            //分页查询
            hospitalSetPage = hospitalSetService.page(Page, wrapper);
        } else {
            hospitalSetPage = hospitalSetService.page(Page);
        }
        return Result.ok(hospitalSetPage);

    }

    //4 添加医院设置
    @PostMapping("/saveHospitalSet")
    @ApiOperation("添加医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "医院设置对象", dataType = "HospitalSet", paramType = "body")
    public Result<?> saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1可用 0不可用
        hospitalSet.setStatus(1);
        //设置Key=加密（毫秒值+1000以内的随机数）
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000));
        hospitalSet.setSignKey(encrypt);
        boolean flag = hospitalSetService.save(hospitalSet);

        if (flag) {
            return Result.ok();
        }
            return Result.fail();
    }
    // 5 根据id获取医院设置
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("/getHospSet/{id}")
    @ApiImplicitParam(name = "id",value = "医院id",dataType = "Long",paramType = "path")
    public Result<HospitalSet> getHospSet(@PathVariable Long id){

//        try {
//           int a = 10/0; 男
//       }catch (Exception e){
//           e.printStackTrace();
//          throw  new RegisterException("不能除以零哦",20001);
//       }


        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHospitalSet")
    @ApiImplicitParam(name = "hospitalSet",value = "医院设置id",dataType = "HospitalSet",paramType = "body")
    public Result<?> updateHospitalSet(@RequestBody HospitalSet hospitalSet){

        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
            return Result.fail();
    }

    //7 批量删除医院设置
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("/batchRemove")
    public Result<?> batchRemoveHospitalSet(@RequestBody List<Long> idList){

        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //8 医院设置锁定和解锁
    @ApiOperation(value = "锁定/解锁医院设置")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "医院id",dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "status",value = "状态",dataType = "integer",paramType = "path")
    })
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }
    //9 发送签名秘钥
    @ApiOperation(value = "发送签名key")
    @PutMapping("/sendKey/{id}")
     @ApiImplicitParam(name = "id",value = "医院id",dataType = "Long",paramType = "path")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //医院
        String hosname = hospitalSet.getHosname();
        //签名Key
        String signKey = hospitalSet.getSignKey();
        // TODO
        return Result.ok();

    }




}
